﻿using System;
using System.Collections.Generic;

namespace Pysics2D
{
    /// <summary>
    /// 用于储存质点位置的数据结构
    /// </summary>
    public struct Location
    {
        public int X { get; set; }
        public int Y { get; set; }
        public Location(int X, int Y)
        {
            this.X = X;
            this.Y = Y;
        }
    }
    /// <summary>
    /// 用于储存质点速度的数据结构
    /// </summary>
    public struct Speed
    {
        public float X { get; set; }
        public float Y { get; set; }
        public Speed(float X,float Y)
        {
            this.X = X;
            this.Y = Y;
        }
    }
    /// <summary>
    /// 质点类
    /// </summary>
    public class Masspoint
    {
        /// <summary>
        /// 设置/返回质点的位置
        /// </summary>
        public Location Location { get; set; }
        /// <summary>
        /// 设置/返回质点的质量
        /// </summary>
        public float M { get; set; }
        /// <summary>
        /// 返回/设置质点的速度
        /// </summary>
        public Speed Speed { get; set; }
        /// <summary>
        /// 设置/返回质点受到的各个分力
        /// </summary>
        public List<Force> Forces = new List<Force>();
        public Masspoint(float M)
        {
            this.M = M;
            Speed = new Speed(0, 0);
        }
        public Masspoint(float M,Speed Speed)
        {
            this.M = M;
            this.Speed = Speed;
        }
        /// <summary>
        /// 在质点上施加一个力
        /// </summary>
        /// <param name="Force">
        /// 要施加的力
        /// </param>
        public void AddForce(Force Force)
        {
            Forces.Add(Force);
        }
        /// <summary>
        /// 获取质点的下一个状态
        /// </summary>
        public void Next()
        {
            float _t = 1F;
            //Location _templocation = new Location();
            foreach (Force _force in Forces)
            {
                float _acceleration = _force.Value / M;//计算加速度
                
                switch (_force.Direction)
                {
                    case 1:     //水平力
                        {
                            Speed = new Speed(Speed.X + _acceleration * _t, Speed.Y);//计算速度
                            break;
                        }
                    case 2:     //竖直力
                        {
                            Speed = new Speed(Speed.X, Speed.Y + _acceleration * _t);//计算速度
                            break;
                        }
                }
            }
            Location = new Location((int)((Location.X + Speed.X * _t) * Environment.Meter+0.5), (int)((Location.Y + Speed.Y * _t) * Environment.Meter+0.5)); //计算新的质点位置
           
        }
    }
    /// <summary>
    /// 力类
    /// </summary>
    public class Force
    {
        /// <summary>
        /// 设置/返回力的大小和线性方向
        /// </summary>
        public float Value { get; set; }
        /// <summary>
        /// 设置/返回力的平面方向.1-水平方向,2-垂直方向
        /// </summary>
        public int Direction { get; set; }
        
        public Force(float Value,int Direction)
        {
            this.Value = Value;
            this.Direction = Direction;
        }
        
    }
    /// <summary>
    /// 物理环境设置
    /// </summary>
    public class Environment
    {
        /// <summary>
        /// 定义一秒为现实中的几秒
        /// </summary>
        public static float Second { get; set; }
        /// <summary>
        /// 定义一米为几像素
        /// </summary>
        public static int Meter { get; set; }
    }
    /// <summary>
    /// 万有引力提供者类
    /// </summary>
    public class GravityProvider
    {

    }
}
